package com.tim.app.ui.fragment.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.base.BaseFragment;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.HorizontalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Answer;
import com.tim.app.ui.activity.selected.AskQuestionActivity;
import com.tim.app.ui.adapter.search.SearchAnswerAdapter;
import com.tim.app.ui.view.SearchAllHeadView;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索的全部页面
 */
public class SearchResultAllFragment extends BaseFragment implements View.OnClickListener, BaseRecyclerAdapter.OnItemClickListener, LoadMoreHandler {

    private static final String TAG = "SearchResultAllFragment";
    private View rootView;

    private WrapRecyclerView rvSearchResult;
    private EmptyLayout emptyLayout;
    private SearchAnswerAdapter adapter;
    private List<Answer> dataList;

    private SearchAllHeadView headView;

    private String keyWord;

    private LoadMoreRecycleViewContainer load_more;

    private RelativeLayout rlBottom;
    private TextView tvGotoAsk;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_search_all_result, container, false);

            rvSearchResult = (WrapRecyclerView) rootView.findViewById(R.id.rvSearchResult);
            rvSearchResult.setOverScrollMode(View.OVER_SCROLL_NEVER);

            load_more = (LoadMoreRecycleViewContainer) rootView.findViewById(R.id.load_more);

            rlBottom = (RelativeLayout) rootView.findViewById(R.id.rlBottom);
            tvGotoAsk = (TextView) rootView.findViewById(R.id.tvGotoAsk);

            load_more.useDefaultFooter(View.GONE);
            load_more.setAutoLoadMore(true);
            load_more.setLoadMoreHandler(this);

            tvGotoAsk.setOnClickListener(this);

            headView = (SearchAllHeadView) inflater.inflate(R.layout.search_all_head_view, null);

            emptyLayout = new EmptyLayout(getActivity(), rvSearchResult);
//            emptyLayout.showLoading();
            emptyLayout.setEmptyText(RT.getString(R.string.no_search_data), "");
            emptyLayout.setEmptyButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    emptyLayout.showLoading();
                    doSearch(keyWord);
                }
            });

            if (dataList == null) {
                dataList = new ArrayList<>();
            }
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvSearchResult.setLayoutManager(layoutManager);
            rvSearchResult.addHeaderView(headView);
            rvSearchResult.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(getResources().getColor(R.color.transparent)).size((int) getResources().getDimension(R.dimen.dimen_10)).build());

            adapter = new SearchAnswerAdapter(getActivity(), dataList);
            adapter.setOnItemClickListener(this);
            rvSearchResult.setAdapter(adapter);
            if (null != getArguments()) {
                keyWord = getArguments().getString(AppKey.SERACH_KEYWORD);
                doSearch(keyWord);
            }
        }
        return rootView;
    }

    @Override
    public void onItemClick(View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvGotoAsk:
                Intent intent = new Intent(getActivity(), AskQuestionActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }

    /**
     * 搜索
     *
     * @param keyword
     */
    public void doSearch(String keyword) {
//        DLOG.e("===", "doSearch");
//        List<User> list1 = new ArrayList<>();
//        List<Topic> list2 = new ArrayList<>();
//        for (int j = 0; j < 3; j++) {
//            User user = new User();
//            user.setName("求余");
//            user.setImage("http://pic.yesky.com/uploadImages/2016/336/18/K3NO6913WQ80.jpg");
//            Topic subject = new Topic();
//            subject.setTopicName("撒撒打算奥斯丁安苏");
//            subject.setTopicDesc("萨岁的萨奥斯丁暗淡奥斯丁安苏s");
//            subject.setImage("http://pic.yesky.com/uploadImages/2016/336/18/K3NO6913WQ80.jpg");
//            subject.setProblemCount(100);
//            subject.setConcernCount(100);
//            list1.add(user);
//            list2.add(subject);
//        }
//        for (int j = 0; j < 8; j++) {
//            SearchInfo info = new SearchInfo();
//            info.setTitle("玩wow是一种怎样的体验");
//            info.setContent("那是非常的爽啊，带你体验不一样的世界");
//            info.setPriseNumber(new Random().nextInt(500));
//            info.setCommentNumber(new Random().nextInt(500));
//            dataList.add(info);
//        }
//        headView.setData(list1, list2);
//        adapter.notifyDataSetChanged();
//        if ((null == list1 || list1.size() == 0)&&(null == list2 || list2.size() == 0)){
//            rlBottom.setVisibility(View.VISIBLE);
//        }else{
//            rlBottom.setVisibility(View.GONE);
//        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(TAG);
    }


}
